package com.yc.yyds.servlet;

import com.google.gson.Gson;
import com.yc.yyds.Bean.Result;
import com.yc.yyds.dao.LinkDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveLinkServlet", value = "/SaveLinkServlet.s")
public class SaveLinkServlet extends HttpServlet {
    LinkDao dao = new LinkDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String linkid = request.getParameter("linkid");
        final String linkname = request.getParameter("linkname");
        final String linkurl = request.getParameter("linkurl");
        final String linkstatus = request.getParameter("linkstatus");
        Result result;

        try{
            if(linkid!=null){
                dao.modify(linkid,linkname,linkurl,linkstatus);
            }else{
                dao.create(linkname,linkurl,linkstatus);
            }
            result= new Result(1,"保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            result= new Result(0,e.getMessage(),null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }

}
